<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class paypal extends template_plugin
{
	var $currency = array(
							'USD' => array("U.S. Dollar",			2,	"\$",		"US\$",		""),
							'CAD' => array("Canadian Dollar",		2,	"\$",		"C\$",		""),
							'AUD' => array("Australian Dollar",		2,	"\$",		"AU\$",		""),
							'EUR' => array("Euro",					2,	"&euro;",	"&euro;",	""),
							'GBP' => array("Pounds Sterling",		2,	"&pound;",	"&pound;",	""),
							'JPY' => array("Yen",					3,	"&yen;",	"&yen;",	""),
							'NZD' => array("New Zealand Dollar",	2,	"\$",		"NZ\$",		""),
							'CHF' => array("Swiss Franc",			2,	"S&#20A3;",	"",			" francs"),
							'HKD' => array("Hong Kong Dollar",		2,	"\$",		"HK\$",		""),
							'SGD' => array("Singapore Dollar",		2,	"\$",		"S\$",		""),
							'SEK' => array("Swedish Krona",			2,	"kr",		"",			" kronor"),
							'DKK' => array("Danish Krone",			2,	"kr",		"",			" kroner"),
							'PLN' => array("Polish Z&#0142;oty",	2,	"z&#0142;",	"",			" z&#0142;"),
							'NOK' => array("Norwegian Krone",		2,	"kr",		"",			" kr"),
							'HUF' => array("Hungarian Forint",		0,	"Ft",		"",			" forint"),
							'CZK' => array("Czech Koruna",			2,	"kr",		"",			" K&#269;"),
							'ILS' => array("Israeli Shekel",		2,	"&#8362;",	"",			" new sheqalim"),
							'MXN' => array("Mexican Peso",			2,	"\$",		"\$",		""),
						);
	
	var $ipsclass;
	
	var $name = "Paypal";
	var $multiple = 0;
	var $cache_array = array('ibm_paypal_rate' => array('Paypal Rates cache','rates_cache'));
	
	var $settings = array();
	
	function format_real_money($amount, $currency = null)
	{
		return $this->_format_real_money($this->ipsclass->numbers->do_number_format($amount), $currency);
	}
	
	function _format_real_money($amount, $currency = null)
	{
		if($currency == null)
		{
			$currency = $this->settings['paypal_currency'];
			if(!$currency)
			{
				$currency = 'USD';
			}
		}
		return $this->currency[$currency][3].$amount.$this->currency[$currency][4];
	}
	
	function rates_cache()
	{
		$this->ipsclass->cache['ibm_paypal_rate'] = array();
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_paypal_rates'));
		$this->ipsclass->DB->exec_query();
		while($rate = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->cache['ibm_paypal_rate'][$rate['rate_id']] = $rate;
		}
		
		$this->ipsclass->update_cache( array( 'name' => 'ibm_paypal_rate', 'array' => 1, 'deletefirst' => 1 ) );
	}
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['paypal_account'] = $this->ipsclass->vars['email_out'];
			$this->settings['paypal_url'] = "www.paypal.com";
			$this->settings['paypal_item_name'] = "Buy ".$this->ipsclass->vars['market_currency'];
			$this->settings['paypal_currency'] = "USD";
		}
	}
	
	function acp_settings()
	{
		// Testing with the Paypal server
		// Validate with Paypal
		$post = array();
		$post[] = 'cmd=_notify-validate';
		
		$verify = implode('&', $post);
		$fp = fsockopen('www.paypal.com', 80, $errno, $errstr, 30);
		if (!$fp)
		{
			$this->ipsclass->admin->error("Unable to communicate with Paypal server. Please make sure that your web server is able to communicate with Paypal");
		}
		stream_set_timeout($fp, 30);
		
		$header  = "POST /cgi-bin/webscr HTTP/1.0\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . strlen($verify) . "\r\n\r\n";
		fputs($fp, $header . $verify);
		while (!feof($fp))
		{
			$ret = fgets($fp, 1024);
		}
		fclose($fp);
		
		if($ret != 'INVALID')
		{
			$this->ipsclass->admin->error("Unable to communicate with Paypal server. Please make sure that your web server is able to communicate with Paypal");
		}
		
		// Settings Portion
		$currencies = array();
		foreach($this->currency as $code => $info)
		{
			$currencies[] = array($code, $info[0]." (".$info[2].")");
		}
		
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Paypal Account</b><br />This paypal account is the email account registered at Paypal and has to be a business account or a premier account.",
															$this->ipsclass->adskin->form_input('paypal_account',$this->settings['paypal_account']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>What Paypal URL to use?</b><br />Only use www.sandbox.paypal.com if you testing with the Paypal Sandbox.",
															$this->ipsclass->adskin->form_dropdown('paypal_url',array(array('www.paypal.com','www.paypal.com'),array('www.sandbox.paypal.com','www.sandbox.paypal.com')),$this->settings['paypal_url']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Paypal Item Name</b><br />This is the name of the item that will appear in Paypal.",
															$this->ipsclass->adskin->form_input('paypal_item_name',$this->settings['paypal_item_name']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Currency to be used</b>",
															$this->ipsclass->adskin->form_dropdown('paypal_currency',$currencies,$this->settings['paypal_currency']),
													)	);
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$this->ipsclass->parser->parse_smilies   = 1;
		$this->ipsclass->parser->parse_html      = 1;
		$this->ipsclass->parser->parse_bbcode    = 1;
		
		$save = array();
		$save['paypal_account'] = $this->ipsclass->input['paypal_account'];
		$save['paypal_url'] = $this->ipsclass->input['paypal_url'];
		$save['paypal_item_name'] = $this->ipsclass->input['paypal_item_name'];
		$save['paypal_currency'] = isset($this->currency[$this->ipsclass->input['paypal_currency']]) ? $this->ipsclass->input['paypal_currency'] : 'USD';
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp_install()
	{
		$this->ipsclass->DB->load_cache_file(ROOT_PATH.'sources/market/sql/'.$this->ipsclass->vars['sql_driver'].'_plugin_queries.php','sql_plugin_queries');
		
		$this->ipsclass->DB->build_query_from_cache('table_paypal_rates',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		$this->ipsclass->DB->build_query_from_cache('table_paypal_logs',array(),'sql_plugin_queries');
		$this->ipsclass->DB->exec_query();
		
		$this->rates_cache();
	}
	
	function acp_uninstall()
	{
		$this->ipsclass->DB->sql_drop_table( 'market_paypal_rates' );
		$this->ipsclass->DB->sql_drop_table( 'market_paypal_logs' );
	}
	
	function acp_menu()
	{
		return array(
					  1 => array('View Paided','code=plugin&do=run&link='.$this->plugin['link']),
					  2 => array('Manage Rates','code=plugin&do=run&link='.$this->plugin['link'].'&action=manage'),
					);
	}
	
	function acp()
	{
		
		$redirect = array(
							'home' => 'view_logs',
							'detail' => 'log_detail',
							'manage' => 'manage_rates',
							'add' => 'add_rate',
							'edit' => 'edit_rate',
							'save' => 'save_rate',
							'delete' => 'delete_rate',
						);
		
		if(!isset($redirect[$this->ipsclass->input['action']]))
		{
			if(!is_array($this->ipsclass->cache['ibm_paypal_rate']) || count($this->ipsclass->cache['ibm_paypal_rate']) == 0)
			{
				$this->ipsclass->input['action'] = 'manage';
			}
			else
			{
				$this->ipsclass->input['action'] = 'home';
			}
		}
		
		$function = 'acp_'.$redirect[$this->ipsclass->input['action']];
		$this->$function();
	}
	
	function acp_view_logs()
	{
		$this->ipsclass->admin->page_title  = "Welcome to log page";
		$this->ipsclass->admin->page_detail = "Welcome to log page of all the points bought using this module";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'PayPal ACP' );
		
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 50;
		
		$total = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_paypal_logs'));
		$page = $this->ipsclass->adskin->build_pagelinks(	array(	'TOTAL_POSS'   => $total['total'],
																	'PER_PAGE'     => $LIMIT,
																	'USE_ST'       => 'st',
																	'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																	'L_SINGLE'     => "Single Page",
																	'BASE_URL'     => $this->ipsclass->base_url."&amp;".$this->ipsclass->form_code
															)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->html .= "<br />";
		
		$this->ipsclass->adskin->td_header[] = array( "Member/Paypal"											, "" );
		$this->ipsclass->adskin->td_header[] = array( "Real Money/".$this->ipsclass->vars['market_currency']	, "" );
		$this->ipsclass->adskin->td_header[] = array( "Time/Payment"											, "" );
		$this->ipsclass->adskin->td_header[] = array( "Details"													, "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Paypal Logs");
		
		$this->ipsclass->DB->build_query(array(
												'select' => 'l.*',
												'from' => array('market_paypal_logs' => 'l'),
												'add_join' => array(
																		array(
																				'select' => 'm.members_display_name, m.mgroup',
																				'from' => array('members' => 'm'),
																				'where' => "l.l_member = m.id",
																				'type' => "left",
																			),
																	),
												'order' => 'l_time DESC',
												'limit' => array($this->ipsclass->input['st'],$LIMIT),
										)	);
		$this->ipsclass->DB->exec_query();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				  $this->ipsclass->market_class->format_name($log['l_member'],$log['members_display_name'],$log['mgroup'])."<br />".$log['l_payer_email'],
																				  $this->format_real_money($log['l_real_money'],$log['l_currency'])."<br />".$this->ipsclass->numbers->format_currency($log['l_money_got']),
																				  $this->ipsclass->get_date($log['l_time'],'LONG')."<br />".$log['l_payment_time'],
																				  "<a href='".$this->ipsclass->base_url."&amp;".$this->ipsclass->form_code."&amp;action=detail&amp;log=".$log['l_id']."'>More Detail</a>",
																		)      );
		}
		if($this->ipsclass->DB->get_num_rows() == 0)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No Paypal logs yet",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_standalone_row($page,"left");
		
		$this->ipsclass->admin->output();
	}
	
	function acp_log_detail()
	{
		$this->ipsclass->input['log'] = intval($this->ipsclass->input['log']);
		
		$log = $this->ipsclass->DB->build_and_exec_query(array(
																'select' => "l.*",
																'from' => array('market_paypal_logs' => 'l'),
																'add_join' => array(
																						array(
																								'select' => 'm.members_display_name, m.mgroup',
																								'from' => array('members' => "m"),
																								'where' => "m.id=l.l_member",
																								'type' => 'left'
																							),
																					),
																'where' => "l.l_id=".$this->ipsclass->input['log']
														)	);
		
		if(!$log)
		{
			$this->ipsclass->admin->error("The log does not exist");
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to log page";
		$this->ipsclass->admin->page_detail = "Welcome to log page of all the points bought using this module";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code, 'PayPal ACP' );
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"	, "" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"	, "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Log Details");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Unique Paypal Transaction ID",
																				$log['l_transaction_id']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Member",
																				$this->ipsclass->market_class->format_name($log['l_member'],$log['members_display_name'],$log['mgroup'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Buyer Real Name",
																				$log['l_first_name']." ".$log['l_last_name']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Buyer Verification",
																				ucfirst($log['l_payer_status'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Buyer Paypal Email",
																				$log['l_payer_email']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Business Email",
																				$log['l_business_email']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Payment Sent To",
																				$log['l_reciever_email']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				$this->ipsclass->vars['market_currency'],
																				$this->ipsclass->numbers->format_currency($log['l_money_got'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				$this->currency[$log['l_currency']][0],
																				$this->format_real_money($log['l_real_money'],$log['l_currency'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"PayPal Fee",
																				$this->format_real_money($log['l_real_fee'],$log['l_currency'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Time",
																				$this->ipsclass->get_date($log['l_time'],'LONG')
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Payment Time",
																				$log['l_payment_time']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Payment Status",
																				$log['l_payment_status']
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																				"Payment Type",
																				ucwords($log['l_payment_type'])
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$rates = unserialize($log['l_rates_bought']);
		
		$this->ipsclass->adskin->td_header[] = array( "Amount"									, "" );
		$this->ipsclass->adskin->td_header[] = array( $this->currency[$log['l_currency']][0]	, "" );
		$this->ipsclass->adskin->td_header[] = array( $this->ipsclass->vars['market_currency']	, "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Rates Bought");
		
		foreach($rates as $rate)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(
																					$this->ipsclass->do_number_format($rate['amount'])."x",
																					$this->format_real_money($rate['real'],$log['l_currency']),
																					$this->ipsclass->numbers->format_currency($rate['money'])
																		 )      );
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function acp_manage_rates()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing rates";
		$this->ipsclass->admin->page_detail = "Welcome to managing rates from real money to digital money.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code."&action=manage", 'Manage Rates' );
		
		if(is_array($this->ipsclass->cache['ibm_paypal_rate']) && count($this->ipsclass->cache['ibm_paypal_rate']))
		{
			$this->ipsclass->adskin->td_header[] = array( $this->currency[$this->settings['paypal_currency']][0]	, "" );
			$this->ipsclass->adskin->td_header[] = array( $this->ipsclass->vars['market_currency']					, "" );
			$this->ipsclass->adskin->td_header[] = array( "Options"													, "" );
			
			$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Currency Rates");
			
			foreach($this->ipsclass->cache['ibm_paypal_rate'] as $rate)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array(  
																					  $this->format_real_money($rate['real_amount']),
																					  $this->ipsclass->numbers->format_currency($rate['money_amount']),
																					  "<img id=\"menu_{$rate['rate_id']}\" src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' border='0' alt='Options' class='ipd' style='float:right'/>",
																			)      );
				
				$this->ipsclass->html .= "<script type=\"text/javascript\">
	menu_build_menu( \"menu_{$rate['rate_id']}\",
  new Array(
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&action=edit&rate_id={$rate['rate_id']}'>Edit Rate</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&action=delete&rate_id={$rate['rate_id']}'>Delete Rate</a>\"
      )     );
 </script>";
			}
			
			$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		}
		
		
		$this->_rate_form(array('rate_id'=>0,'real_amount'=>1,'money_amount'=>1));
		
		$this->ipsclass->admin->output();
	}
	
	function _rate_form($rate)
	{
		$this->ipsclass->html .= $this->_acp_start_form(array(
																array('action',$rate['rate_id'] ? 'save' : 'add'),
																array('rate_id',$rate['rate_id'])
														)	);
		
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}" , "60%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table($rate['rate_id'] ? "Edit a Conversion Rate" : "Add a Conversion Rate");
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>How much</b><br />The name of the company or corporation. This does not have to be the same name. It can be made up.",
																			  $this->_format_real_money($this->ipsclass->adskin->form_input("real_amount",$rate['real_amount']))
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>Real Stock Symbol</b><br />This is the stock symbol used in the real world. It will uses the stock quote from this symbol to simulate the stock market. The stock quotes are gathered from <a href='http://finance.yahoo.com/' target='_blank'>Yahoo! Fiannace</a>. This will not be visible to the users",
																			  $this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_input("money_amount",$rate['money_amount']))
																	 )      );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form($rate['rate_id'] ? "Save Rate" : "Add Rate");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
	}
	
	function acp_add_rate()
	{
		$rate = array();
		
		$rate['real_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['real_amount'],'float');
		$rate['real_amount'] = round($rate['real_amount'], $this->currency[$this->settings['paypal_currency']][1]);
		$rate['money_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['money_amount']);
		if($this->ipsclass->numbers->compare($rate['real_amount'],0) <= 0)
		{
			$this->ipsclass->admin->error("You must specify an real money amount greater than 0");
		}
		if($this->ipsclass->numbers->compare($rate['money_amount'],0) <= 0)
		{
			$this->ipsclass->admin->error("You must specify ".$this->ipsclass->vars['market_currency']." amount greater than 0");
		}
		
		$this->ipsclass->DB->do_insert('market_paypal_rates',$rate);
		
		$this->rates_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=manage","Conversion Rate created...");
	}
	
	function acp_edit_rate()
	{
		if(!is_array($this->ipsclass->cache['ibm_paypal_rate'][$this->ipsclass->input['rate_id']]))
		{
			$this->ipsclass->admin->error("Cannot find rate");
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to editing a rate";
		$this->ipsclass->admin->page_detail = "Welcome to editing a rate from real money to digital money.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code."&action=manage", 'Manage Rates' );
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code."&action=edit&rate_id=".$this->ipsclass->input['rate_id'], 'Edit Rate' );
		
		$this->_rate_form($this->ipsclass->cache['ibm_paypal_rate'][$this->ipsclass->input['rate_id']]);
		
		$this->ipsclass->admin->output();
	}
	
	function acp_save_rate()
	{
		if(!is_array($this->ipsclass->cache['ibm_paypal_rate'][$this->ipsclass->input['rate_id']]))
		{
			$this->ipsclass->admin->error("Cannot find rate");
		}
		
		$rate = array();
		
		$rate['real_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['real_amount'],'float');
		$rate['real_amount'] = round($rate['real_amount'], $this->currency[$this->settings['paypal_currency']][1]);
		$rate['money_amount'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['money_amount']);
		
		$this->ipsclass->DB->do_update('market_paypal_rates', $rate, "rate_id=".$this->ipsclass->input['rate_id']);
		
		$this->rates_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=manage","Conversion Rate saved...");
	}
	
	function acp_delete_rate()
	{
		if(!is_array($this->ipsclass->cache['ibm_paypal_rate'][$this->ipsclass->input['rate_id']]))
		{
			$this->ipsclass->admin->error("Cannot find rate");
		}
		
		$this->ipsclass->DB->do_delete('market_paypal_rates', "rate_id=".$this->ipsclass->input['rate_id']);
		
		$this->rates_cache();
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&action=manage","Conversion Rate deleted...");
	}
	
	function run()
	{
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		if(!is_array($this->ipsclass->cache['ibm_paypal_rate']) || count($this->ipsclass->cache['ibm_paypal_rate']) == 0)
		{
			$this->ipsclass->Error(array('MSG'=>'paypal_not_setup'));
		}
		
		$this->ipsclass->market_class->title[] = array($this->plugin['name'],'market_'.$this->plugin['link']);
		if(in_array($this->ipsclass->member['mgroup'],$this->plugin['use_permission']))
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['paypal_buy_money']."</a></b>",
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}&amp;do=logs'>".$this->ipsclass->lang['paypal_list_logs']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_rates',
									'checkout' => 'buy_money',
									'logs' => 'logs_bought',
									'detail' => 'detail_log',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		else
		{
			$this->ipsclass->market_class->menu[] = array(
															"<b><a href='{$this->ipsclass->market_class->base_url}plugin={$this->plugin['link']}'>".$this->ipsclass->lang['paypal_list_rates']."</a></b>",
														);
			
			$do_redirect = array(
									'list' => 'list_rates',
								  );
			
			if(!isset($do_redirect[$this->ipsclass->input['do']]))
			{
				$this->ipsclass->input['do'] = 'list';
			}
		}
		
		$func = 'run_'.$do_redirect[$this->ipsclass->input['do']];
		$this->$func();
	}
	
	function run_list_rates()
	{
		$this->ipsclass->lang['paypal_final_real'] = sprintf($this->ipsclass->lang['paypal_total_s'],$this->currency[$this->settings['paypal_currency']][0]);
		$this->ipsclass->lang['paypal_final_money'] = sprintf($this->ipsclass->lang['paypal_total_s'],$this->ipsclass->vars['market_currency']);
		
		foreach($this->ipsclass->cache['ibm_paypal_rate'] as $r_id => $rate)
		{
			$this->ipsclass->cache['ibm_paypal_rate'][$r_id]['_real_amount'] = $this->format_real_money($rate['real_amount']);
			$this->ipsclass->cache['ibm_paypal_rate'][$r_id]['_money_amount'] = $this->ipsclass->numbers->format_currency($rate['money_amount']);
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->paypal_list_rate($this->currency[$this->settings['paypal_currency']]);
	}
	
	function run_buy_money()
	{
		$rates = array();
		$_rates = array();
		$real = 0;
		$money = 0;
		
		foreach($this->ipsclass->cache['ibm_paypal_rate'] as $r_id => $rate)
		{
			$this->ipsclass->input['rate'][$r_id] = intval($this->ipsclass->input['rate'][$r_id]);
			
			if($this->ipsclass->input['rate'][$r_id] > 0)
			{
				$real = $this->ipsclass->numbers->add($real, $this->ipsclass->numbers->multiply($rate['real_amount'],$this->ipsclass->input['rate'][$r_id]));
				$money = $this->ipsclass->numbers->add($money, $this->ipsclass->numbers->multiply($rate['money_amount'],$this->ipsclass->input['rate'][$r_id]));
				$rates[$r_id] = array('amount' => $this->ipsclass->numbers->do_number_format($this->ipsclass->input['rate'][$r_id]), 'real' => $this->format_real_money($rate['real_amount']), 'money' => $this->ipsclass->numbers->format_currency($rate['money_amount']));
				$_rates[$r_id] = array('amount' => $this->ipsclass->input['rate'][$r_id], 'real' => $rate['real_amount'], 'money' => $rate['money_amount']);
			}
		}
		
		if(!$real && !$money)
		{
			$this->ipsclass->Error(array('MSG'=>"paypal_buy_none"));
		}
		
		$base64 = base64_encode(serialize($_rates));
		$salt = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members_converge','where'=>"converge_id=".$this->ipsclass->member['id']));
		$key = md5($this->ipsclass->member['id'] . $base64 . $salt['converge_pass_salt'] . $salt['converge_pass_hash'] . $this->ipsclass->vars['sql_pass']);
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->paypal_checkout($this->settings['paypal_url'], $this->settings, $this->currency[$this->settings['paypal_currency']], $rates, $real, $this->format_real_money($real), $this->ipsclass->numbers->format_currency($money), $base64, $key);
	}
	
	function run_logs_bought()
	{
		$this->ipsclass->input['st'] = intval($this->ipsclass->input['st']);
		$LIMIT = 25;
		
		$rows = $this->ipsclass->DB->build_and_exec_query(array('select'=>'COUNT(*) as total','from'=>'market_paypal_logs','where'=>"l_member=".$this->ipsclass->member['id']));
		$page_links = $this->ipsclass->build_pagelinks(	array(	'TOTAL_POSS'   => $rows['total'],
																'PER_PAGE'     => $LIMIT,
																'CUR_ST_VAL'   => $this->ipsclass->input['st'],
																'L_SINGLE'     => $this->ipsclass->compiled_templates['skin_global']->pagination_compile($this->ipsclass->compiled_templates['skin_global']->pagination_make_jump(),"","",$this->ipsclass->compiled_templates['skin_global']->pagination_current_page(1),"","",1,$LIMIT,$this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=logs",0,'st'),
																'BASE_URL'     => $this->ipsclass->market_class->base_url."plugin={$this->ipsclass->input['plugin']}&amp;do=logs",
																'USE_ST'       => 'st'
														)	);
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_paypal_logs','where'=>"l_member=".$this->ipsclass->member['id'],'order'=>"l_time DESC",'limit'=>array($this->ipsclass->input['st'],$LIMIT)));
		$this->ipsclass->DB->exec_query();
		$logs = array();
		while($log = $this->ipsclass->DB->fetch_row())
		{
			$log['real'] = $this->format_real_money($log['l_real_money'],$log['l_currency']);
			$log['money'] = $this->ipsclass->numbers->format_currency($log['l_money_got']);
			$log['recieved'] = $this->ipsclass->get_date($log['l_time'],'LONG');
			$logs[$log['l_id']] = $log;
		}
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->paypal_list_logs($logs,$page_links);
	}
	
	function run_detail_log()
	{
		$this->ipsclass->input['log'] = intval($this->ipsclass->input['log']);
		$log = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'market_paypal_logs','where'=>"l_id=".$this->ipsclass->input['log']));
		
		if(!$log || $log['l_member'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'incorrect_use'));
		}
		
		$log['rates'] = unserialize($log['l_rates_bought']);
		foreach($log['rates'] as $r_id => $rate)
		{
			$log['rates'][$r_id]['amount']  = $this->ipsclass->do_number_format($rate['amount']);
			$log['rates'][$r_id]['real'] = $this->format_real_money($rate['real'],$log['l_currency']);
			$log['rates'][$r_id]['money'] = $this->ipsclass->numbers->format_currency($rate['money']);
		}
		
		$log['real'] = $this->format_real_money($log['l_real_money'],$log['l_currency']);
		$log['money'] = $this->ipsclass->numbers->format_currency($log['l_money_got']);
		$log['recieved'] = $this->ipsclass->get_date($log['l_time'],'LONG');
		
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->paypal_log_detail($log,$this->currency[$log['l_currency']]);
	}
}
?>